﻿using UnityEngine;
using System.IO;
using System.Collections.Generic;
using ZyGame.Drawing;
using System;
using ZyGame.Execute;

namespace ZyGame
{
    public static class Extension
    {
        public static T GetAndCast<T>(this object[] list)
        {
            if (list is null || list.Length is 0)
            {
                return default;
            }

            for (int i = 0; i < list.Length; i++)
            {
                if (list[i] is T)
                {
                    return (T)list[i];
                }
            }

            return default;
        }

        public static T GetAndCast<T>(this object[] list, T def)
        {
            if (list is null || list.Length is 0)
            {
                return def;
            }

            for (int i = 0; i < list.Length; i++)
            {
                if (list[i] is T)
                {
                    return (T)list[i];
                }
            }

            return def;
        }

        public static T GetAndCast<T>(this object[] list, int index)
        {
            if (list is null || list.Length is 0 || index >= list.Length)
            {
                return default;
            }

            if (list[index] is T)
            {
                return (T)list[index];
            }

            return default;
        }

        public static T CastOrDefault<T>(this object[] list, int index, T def)
        {
            if (list is null || list.Length is 0 || index >= list.Length)
            {
                return def;
            }

            if (list[index] is not null && list[index] is T)
            {
                return (T)list[index];
            }

            return def;
        }

        public static Texture2D Combines(this List<DrawingData> layers, Texture2D org)
        {
            RenderTexture render = new RenderTexture(layers[0].width, layers[0].height, 0, RenderTextureFormat.Default);
            render.Clear();
            render.DrawTexture(new Rect(0, 0, org.width, org.height), org, null);
            for (int i = 0; i < layers.Count; i++)
            {
                render.DrawTexture(new Rect(0, 0, layers[i].width, layers[i].height), layers[i].texture, null);
            }

            return render.ReadTexture2D();
        }

        public static Color ReadColor(this BinaryReader reader)
        {
            Color color = Color.white;
            color.r = reader.ReadSingle();
            color.g = reader.ReadSingle();
            color.b = reader.ReadSingle();
            color.a = reader.ReadSingle();
            return color;
        }

        public static void Write(this BinaryWriter writer, Color color)
        {
            writer.Write(color.r);
            writer.Write(color.g);
            writer.Write(color.b);
            writer.Write(color.a);
        }

        public static string GetTimeString(this DateTime time)
        {
            return time.ToString("yyyyMMddHHmmssffff");
        }

        public static T GetObject<T>(this Runnable<string> runnable) where T : class
        {
            if (runnable.EnsureExecutedSuccessful() is false)
            {
                Client.Console.WriteError(runnable.result);
                return default;
            }

            runnable.result.TryGetJson(out T result);
            return result;
        }
    }

    public enum Service
    {
        NONE,
        OSS,
        COS,
    }

    [Serializable]
    public class UploadConfig
    {
        [Header("是否启用")] public bool isOn = false;

        [Header("服务商")] public Service service;
        
        [Header("别称")] public string name;

        [Header("接口地址")] public string url;

        [Header("项目ID")] public string pid;

        [Header("")] public string sid;

        [Header("授权码")] public string token;
    }
}